/**
 * 
 */
package kr.pe.johnkim.dayve.provider;

import java.util.ArrayList;

import kr.pe.johnkim.dayve.activity.R;
import kr.pe.johnkim.dayve.provider.DayveCategoryAdapter.ViewWrapper;
import kr.pe.johnkim.dayve.util.DayveCommon;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Category 분석 화면에서 item 목록을 출력하는 Adapter Class 이다.
 * 
 * @author johnkim
 *
 */
public class DayveCategoryAnalysisAdapter extends BaseAdapter {
	
	public static final String TAG = DayveCategoryAnalysisAdapter.class.getSimpleName();
	
	private Context context;
	private ArrayList<DayveItem> data;
	private DayveItem item;
	LayoutInflater inf;
	
	View view;
	
	DayveCommon dayveCommon = new DayveCommon();
	
	public DayveCategoryAnalysisAdapter(Context context, ArrayList<DayveItem> data) {
		this.context = context;
		this.data = data;
		inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		view = convertView;
		
		ViewWrapper wrapper = null;
		
		if(view == null) {
			view = inf.inflate(R.layout.dayve_category_analysis_row, parent, false);
			wrapper = new ViewWrapper(view);
			view.setTag(wrapper);
		} else {
			wrapper = (ViewWrapper)view.getTag();
		}
		
		item = data.get(position);
		
		// 배경 컬러를 세팅 한다.
//		wrapper.getLinearLayoutCategoryAnalysisRowItemName1().setBackgroundColor(Color.rgb(188, 188, 188));
//		wrapper.getLinearLayoutCategoryAnalysisRowItemName2().setBackgroundColor(Color.rgb(188, 188, 188));
//		wrapper.getLinearLayoutCategoryAnalysisRowItemAmount1().setBackgroundColor(Color.rgb(242, 242, 242));
//		wrapper.getLinearLayoutCategoryAnalysisRowItemAmount2().setBackgroundColor(Color.rgb(242, 242, 242));
		
		// item name 을 세팅 한다.
		wrapper.getTxtViewAnalysisCategoryItemName().setText("  " + item.getName() + "  ");
		// item amount 를 세팅 한다.
		wrapper.getTxtViewAnalysisCategoryItemAmount().setText(item.getAmount());
		// item register date 를 세팅 한다.
		wrapper.getTxtViewAnalysisCategoryItemRegisterDate().setText("    " + dayveCommon.getSimpleDateFormat(item.getModifyDate()) + "  ");
		
		return view;
	}

	/**
	 * View Wrapper Class.
	 * 
	 * @author johnkim
	 *
	 */
	class ViewWrapper {
		View base;

		LinearLayout linearLayoutCategoryAnalysisRowItemName1;
		LinearLayout linearLayoutCategoryAnalysisRowItemName2;
		LinearLayout linearLayoutCategoryAnalysisRowItemAmount1;
		LinearLayout linearLayoutCategoryAnalysisRowItemAmount2;
		
		TextView txtViewAnalysisCategoryItemName;
		TextView txtViewAnalysisCategoryItemAmount;
		TextView txtViewAnalysisCategoryItemRegisterDate;
		
		ViewWrapper(View base) {
			this.base = base;
		}
		
		LinearLayout getLinearLayoutCategoryAnalysisRowItemName1() {
			if(linearLayoutCategoryAnalysisRowItemName1 == null) {
				linearLayoutCategoryAnalysisRowItemName1 = (LinearLayout)view.findViewById(R.id.linearLayoutCategoryAnalysisRowItemName1);
			}
			
			return (linearLayoutCategoryAnalysisRowItemName1);
		}
		
		LinearLayout getLinearLayoutCategoryAnalysisRowItemName2() {
			if(linearLayoutCategoryAnalysisRowItemName2 == null) {
				linearLayoutCategoryAnalysisRowItemName2 = (LinearLayout)view.findViewById(R.id.linearLayoutCategoryAnalysisRowItemName2);
			}
			
			return (linearLayoutCategoryAnalysisRowItemName2);
		}
		
		LinearLayout getLinearLayoutCategoryAnalysisRowItemAmount1() {
			if(linearLayoutCategoryAnalysisRowItemAmount1 == null) {
				linearLayoutCategoryAnalysisRowItemAmount1 = (LinearLayout)view.findViewById(R.id.linearLayoutCategoryAnalysisRowItemAmount1);
			}
			
			return (linearLayoutCategoryAnalysisRowItemAmount1);
		}
		
		LinearLayout getLinearLayoutCategoryAnalysisRowItemAmount2() {
			if(linearLayoutCategoryAnalysisRowItemAmount2 == null) {
				linearLayoutCategoryAnalysisRowItemAmount2 = (LinearLayout)view.findViewById(R.id.linearLayoutCategoryAnalysisRowItemAmount2);
			}
			
			return (linearLayoutCategoryAnalysisRowItemAmount2);
		}
		
		TextView getTxtViewAnalysisCategoryItemName() {
			if(txtViewAnalysisCategoryItemName == null) {
				txtViewAnalysisCategoryItemName = (TextView)view.findViewById(R.id.txtViewAnalysisCategoryItemName);
			}
			
			return (txtViewAnalysisCategoryItemName);
		}
		
		TextView getTxtViewAnalysisCategoryItemAmount() {
			if(txtViewAnalysisCategoryItemAmount == null) {
				txtViewAnalysisCategoryItemAmount = (TextView)view.findViewById(R.id.txtViewAnalysisCategoryItemAmount);
			}
			
			return (txtViewAnalysisCategoryItemAmount);
		}
		
		TextView getTxtViewAnalysisCategoryItemRegisterDate() {
			if(txtViewAnalysisCategoryItemRegisterDate == null) {
				txtViewAnalysisCategoryItemRegisterDate = (TextView)view.findViewById(R.id.txtViewAnalysisCategoryItemRegisterDate);
			}
			
			return (txtViewAnalysisCategoryItemRegisterDate);
		}
	}
}
